<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EntityList
 *
 * @author Teodor
 */
class EntityList extends CWidget{
    public $template = "<tr>
                         <td class='data'><input type='hidden' name='Characteristics[{id}][id]' value='{id}'>
                                          <input type='hidden' name='Characteristics[{id}][name]' value='{name}'>
                         </td>
                         <td class='name'>{name}</td>
                         <td><img src='{image}'/></td>
                        </tr>";
    public $imageUrl;
    
    public $label;
    
    public $optionsData;
    
    public $listData;
    
    public $optionsId;
    
    public $css;


    public function init() 
    {
         if(!isset($this->css))
          {
            $cssFile = dirname(__FILE__)."/assets/css/entitylist.css";
            $this->css = Yii::app()->getAssetManager()->publish($cssFile);
          }
         if(!isset($this->imageUrl))
          {
            $imgFile = dirname(__FILE__)."/assets/images/delete.png";
            $this->imageUrl = Yii::app()->getAssetManager()->publish($imgFile);
          }
           $this->template = str_replace("{image}", $this->imageUrl, $this->template);
    }
    
    public function run() {
        $this->registerScripts();
        $this->renderContent();
    }
    
    protected function registerScripts()
    {
        $cs = Yii::app()->clientScript;       
        $cs->registerCssFile($this->css);
        $cs->registerCoreScript("jquery");
        $cs->registerScript("template",'var template = "'.str_replace(array("\n", "\r"), '', $this->template).'";');
        $cs->registerScript("bindClickEvent",'$("div#entityList table").delegate("td>img", "click", onDeleteClick)');
        $cs->registerScript("addItem",'$("#AddButton").click(function(){ 
                                            var newVal = $("#'.$this->optionsId.'>option:selected");
                                            var id     = newVal.val();
                                            var name   = newVal.text();      
                                             if(!id || $("div#entityList td>input:hidden[value="+id+"]").length){
                                                return;
                                            }
                                            addNewItem(template, id, name);
                                        });

                                        function addNewItem(newItemTmp, id, name){
                                            newItemTmp = newItemTmp.replace(/\{id\}/g, id);
                                            newItemTmp = newItemTmp.replace(/\{name\}/g, name);
                                            $(newItemTmp).appendTo("div#entityList table");
                                        }

                                        function onDeleteClick(){
                                           $(this).closest("tr").fadeOut("slow", function(){
                                                    $(this).remove();
                                                    }); 
                                        }');
        
        
    }

    protected function renderContent()
    {
        echo CHtml::openTag('div', array('class'=>'row'));
            echo CHtml::label($this->label, $this->optionsId);
            echo CHtml::dropDownList($this->optionsId, '', $this->optionsData);
            echo CHtml::button('Add', array('id'=>'AddButton'));
        echo CHtml::closeTag('div'); //-row
        
        echo CHtml::openTag('div', array('id'=>'entityList'));
            echo CHtml::openTag('table');
            foreach($this->listData as $data)
            {
                $tmp = str_replace('{id}', $data->id, $this->template);
                echo str_replace('{name}', $data->name, $tmp);
            }               
            echo CHtml::closeTag('table');
        echo CHtml::closeTag('div');  
    }
}

 ?>
